// pages/sell_addcustomer/sell_addcustomer.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    name: '',
    phone: '',
    address: [],
    inputaddress: '',
    index: 0,
    shopid: 0,
    flag: 0
    //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框  
  },
  //点击按钮痰喘指定的hiddenmodalput弹出框  
  add_address: function () {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput,
      inputaddress: ''
    })
  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function () {
    var tm = []
    var tm2 = []
    this.data.address.push({ name: this.data.inputaddress })
    this.setData({
      index: this.data.index + 1
    })
    console.log(this.data.index);
    console.log('wzh' + this.data.address[0].name)
    tm = this.data.address
    this.setData({
      hiddenmodalput: true,
      address: tm
    })
    console.log('ggg' + this.data.address)//当前已添加所有的地址
  },

  getvalue: function (e) {//获取输入地址
    this.setData({
      inputaddress: e.detail.value
    })
    console.log(this.data.inputaddress)
  },
  save: function (e) {
    this.setData({
      name: e.detail.value.name,
      phone: e.detail.value.phone,
      shopid: wx.getStorageSync("shopid")
    })
    var index = 0;
    console.log(this.data.index);
    var that = this;

    wx.request({
      url: 'https://www.wenzhixinxi.com/phptest/insertuser.php', //你的后台地址
      data: {
        shopid: that.data.shopid,
        name: that.data.name,
        phone: that.data.phone,
      },
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {
        console.log('用户基本信息插入');
        var index = 0;
        console.log(that.data.index)
        console.log(that.data.address[index].name);
        // if(that.data.address!=null)
        // {
        while (index < that.data.index) {
          wx.request({
            url: 'https://www.wenzhixinxi.com/phptest/insertaddress.php', //你的后台地址
            data: {
              shopid: that.data.shopid,
              address: that.data.address[index].name
            },
            header: {
              'content-type': 'application/json'
            },

            success: function (res) {
              console.log('地址插入成功');

            }
          })
          index++;
        }
        // }
        console.log('出来了');
        wx.showModal({
          title: '提示',
          content: '地址插入成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')

              wx.switchTab({
                url: '../sell_customer/sell_customer',
                success: function (e) {
                  var page = getCurrentPages().pop();
                  if (page == undefined || page == null) return;
                  page.onLoad();
                }
              })
            }
          }
        })
      }
    })


  }
})